package com.sy.simpleegg.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.sy.simpleegg.room.entity.SaveCoinEntity

@Dao
interface SaveCoinDao {
    @Query("SELECT * FROM tb_save_coin")
    fun getAll(): MutableList<SaveCoinEntity>?
    
    @Query("SELECT * FROM tb_save_coin WHERE sn = :sn ORDER BY id DESC LIMIT 1")
    fun getBySn(sn: String): MutableList<SaveCoinEntity>?

    @Query("SELECT * FROM tb_save_coin WHERE step = 1 AND coins > 0 ORDER BY id DESC")
    fun getCanReport(): MutableList<SaveCoinEntity>?

    @Query("UPDATE tb_save_coin SET step = :step WHERE sn = :sn")
    fun updateStepBySn(sn: String, step: Int)

    @Update
    fun update(saveCoinEntity: SaveCoinEntity)

    @Query("UPDATE tb_save_coin SET coins = :coins WHERE sn = :sn")
    fun update(sn: String, coins: String)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(saveCoinEntity: SaveCoinEntity)

    @Transaction
    fun insertAll(outEntities: MutableList<SaveCoinEntity>){
        outEntities.forEach {
            insert(it)
        }
    }

    @Delete
    fun delete(saveCoinEntity: SaveCoinEntity)

    @Query("DELETE FROM tb_save_coin")
    fun deleteAll()

    @Query("DELETE FROM tb_save_coin where sn = :sn")
    fun deleteBySn(sn: String)
}